package com.jl.card.provider.jl2g;

import java.util.Iterator;

import com.jl.card.Card;
import com.jl.card.file.CardFilePath;
import com.jl.card.file.IFileChecker;
import com.jl.util.HexUtil;

public final class JL2GFileChecker implements IFileChecker {

	private final static byte[] cmd = new byte[] { (byte) 0xa0, (byte) 0xa4, 0x00,
			0x00, 0x02, 0x00, 0x00 };

	private final static byte ok1 = (byte) 0x90;
	private final static byte ok2 = (byte) 0x9f;

	@Override
	public boolean checkFromMF(Card card, CardFilePath path) {
		Iterator<String> name = path.iterator();
		byte[] res = null;
		while (name.hasNext()) {
			String type = name.next();
			byte[] temp = HexUtil.parseHexToByteArray(type);
			cmd[5] = temp[0];
			cmd[6] = temp[1];
			res = card.plainAPDU(cmd);
		}
		if (res == null) {
			return false;
		} else {
			if (res[res.length - 2] == ok1 || res[res.length - 2] == ok2) {
				return true;
			}
			return false;
		}
	}

	@Override
	public boolean checkUnderCurrent(Card card, short name) {
		cmd[5] = HexUtil.highByte(name);
		cmd[6] = HexUtil.lowByte(name);
		byte res[] = card.plainAPDU(cmd);
		return res[0] == ok1 || res[0] == ok2;
	}

}
